package edu.thu.studio.ormdesigner.editor.action;

import java.util.List;

import edu.thu.studio.ormdesigner.model.diagram.Diagram;
import edu.thu.studio.ormdesigner.model.diagram.node.Node;
import edu.thu.studio.ormdesigner.model.orm.table.Column;
import edu.thu.studio.ormdesigner.model.orm.table.Key;
import edu.thu.studio.ormdesigner.model.orm.table.ReferenceSet;
import edu.thu.studio.ormdesigner.model.orm.table.Table;
import edu.thu.studio.ormdesigner.model.orm.view.View;
import edu.thu.studio.ormdesigner.util.Utils;

public class CopyManager {
    
    private static int numberOfCopy;
    
    private static Node originNode;
    
    public CopyManager() {
    }
    
    public static void clear() {
        numberOfCopy = 0;
        originNode = null;
    }
    
    public static void copy(Node node) {
        originNode = node;
    }
    
    public static Node getOriginNode() {
        return originNode;
    }
    
    public static Node paste() {
        numberOfCopy++;
        Node node = originNode.clone();
        
        node.setId(Utils.getUUID());
        node.setXY(node.getX() + numberOfCopy * 50, node.getY() + numberOfCopy * 50);
        
        return node;
    }
    
    public static Node pasteAsShortCut() {
        numberOfCopy++;
        Node node = originNode.clone();
        node.setOriginal(false);
        node.setId(Utils.getUUID());
        node.setXY(node.getX() + numberOfCopy * 50, node.getY() + numberOfCopy * 50);
        // node.setDiagram(originNode.getDiagram());
        
        return node;
    }
    
    public static View pasteAsView() {
        View view = Utils.getView(originNode.getRefId());
        if (null != view) {
            View clone = (View) view.cloneAsNew();
            clone.setCode(view.getCode() + "_" + numberOfCopy);
            clone.setName(view.getName() + "_" + numberOfCopy);
            return clone;
        }
        return null;
    }
    
    public static Table pasteAsTable() {
        Table table = Utils.getTable(originNode.getRefId());
        if (null != table) {
            Table clone = (Table) table.cloneAsNew();
            clone.setCode(table.getCode() + "_" + numberOfCopy);
            clone.setName(table.getName() + "_" + numberOfCopy);
            
            clone.setGenerateCode(true);
            
            clone.setKey(new Key());
            clone.setReferences(new ReferenceSet());
            
            List<Column> columnList = clone.getColumns().getObjectList();
            for (Column column : columnList) {
                column.setPrimary(false);
                column.setMandatory(true);
            }
            
            return clone;
        }
        return null;
    }
    
    public static boolean canPaste() {
        // 只允许复制表和视图，不能复制快照
        if (null != originNode) {
            return originNode.isOriginal();
        }
        return false;
    }
    
    public static boolean canPasteShortCut(Diagram diagram) {
        // 只允许表和视图创建快照，不能由快照创建快照
        if (canPaste()) {
            return true;
        }
        return false;
    }
}
